"""初始化电影数据管理命令

用于向数据库中添加示例电影数据和用户数据。
"""

import json
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from movies.models import Movie

User = get_user_model()


class Command(BaseCommand):
    """初始化电影数据命令类"""

    help = "向数据库中添加示例电影数据和用户数据"

    def add_arguments(self, parser):
        """添加命令行参数"""
        parser.add_argument(
            "--clear",
            action="store_true",
            help="清除现有数据后再添加",
        )

    def handle(self, *args, **options):
        """
        处理命令逻辑

        Args:
            *args: 位置参数
            **options: 命令行选项
        """
        if options["clear"]:
            self.stdout.write("正在清除现有电影数据...")
            Movie.objects.all().delete()
            self.stdout.write("电影数据已清除。")

        # 创建管理员用户
        admin_user, created = User.objects.get_or_create(
            username="admin",
            defaults={
                "email": "admin@example.com",
                "is_staff": True,
                "is_superuser": True,
            },
        )
        if created:
            admin_user.set_password("admin123")
            admin_user.save()
            self.stdout.write('管理员用户 "admin" 已创建，密码为 "admin123"')
        else:
            self.stdout.write('管理员用户 "admin" 已存在')

        # 创建普通用户
        users_data = [
            {"username": "user1", "email": "user1@example.com"},
            {"username": "user2", "email": "user2@example.com"},
        ]

        for user_data in users_data:
            user, created = User.objects.get_or_create(
                username=user_data["username"],
                defaults=user_data,
            )
            if created:
                user.set_password("user123")
                user.save()
                self.stdout.write(f'普通用户 "{user_data["username"]}" 已创建，密码为 "user123"')
            else:
                self.stdout.write(f'普通用户 "{user_data["username"]}" 已存在')

        # 创建电影数据
        movies_data = [
            {
                "title": "肖申克的救赎",
                "genres": ["剧情", "犯罪"],
                "country": "美国",
                "release_year": 1994,
                "extra_data": {
                    "directors": ["弗兰克·德拉邦特"],
                    "actors": ["蒂姆·罗宾斯", "摩根·弗里曼"],
                },
            },
            {
                "title": "霸王别姬",
                "genres": ["剧情", "爱情"],
                "country": "中国",
                "release_year": 1993,
                "extra_data": {
                    "directors": ["陈凯歌"],
                    "actors": ["张国荣", "张丰毅", "巩俐"],
                },
            },
            {
                "title": "阿甘正传",
                "genres": ["剧情", "爱情"],
                "country": "美国",
                "release_year": 1994,
                "extra_data": {
                    "directors": ["罗伯特·泽米吉斯"],
                    "actors": ["汤姆·汉克斯", "罗宾·怀特"],
                },
            },
            {
                "title": "泰坦尼克号",
                "genres": ["剧情", "爱情", "灾难"],
                "country": "美国",
                "release_year": 1997,
                "extra_data": {
                    "directors": ["詹姆斯·卡梅隆"],
                    "actors": ["莱昂纳多·迪卡普里奥", "凯特·温斯莱特"],
                },
            },
            {
                "title": "这个杀手不太冷",
                "genres": ["剧情", "动作", "犯罪"],
                "country": "法国",
                "release_year": 1994,
                "extra_data": {
                    "directors": ["吕克·贝松"],
                    "actors": ["让·雷诺", "娜塔莉·波特曼"],
                },
            },
            {
                "title": "美丽人生",
                "genres": ["剧情", "喜剧"],
                "country": "意大利",
                "release_year": 1997,
                "extra_data": {
                    "directors": ["罗伯托·贝尼尼"],
                    "actors": ["罗伯托·贝尼尼", "尼可莱塔·布拉斯基"],
                },
            },
            {
                "title": "千与千寻",
                "genres": ["动画", "奇幻"],
                "country": "日本",
                "release_year": 2001,
                "extra_data": {
                    "directors": ["宫崎骏"],
                    "actors": ["柊瑠美", "入野自由"],
                },
            },
            {
                "title": "辛德勒的名单",
                "genres": ["剧情", "历史", "战争"],
                "country": "美国",
                "release_year": 1993,
                "extra_data": {
                    "directors": ["史蒂文·斯皮尔伯格"],
                    "actors": ["连姆·尼森", "本·金斯利"],
                },
            },
        ]

        created_count = 0
        for movie_data in movies_data:
            movie, created = Movie.objects.get_or_create(
                title=movie_data["title"],
                country=movie_data["country"],
                release_year=movie_data["release_year"],
                defaults=movie_data,
            )
            if created:
                created_count += 1

        self.stdout.write(
            self.style.SUCCESS(
                f"成功添加 {created_count} 部电影数据，总共 {Movie.objects.count()} 部电影。"
            )
        )
        self.stdout.write(
            "可用用户：\n"
            "- 管理员: admin / admin123\n"
            "- 普通用户1: user1 / user123\n"
            "- 普通用户2: user2 / user123"
        )
